package com.dqhl.community.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.dqhl.community.R;
import com.dqhl.community.model.OrderRecord;
import com.dqhl.community.util.Config;
import com.dqhl.community.util.ViewHolder;

import org.xutils.x;

import java.util.List;

/**
 * Created by mac on 16/11/18.
 * 单个订单adapter
 */
public class OrderSingleListAdapter extends BaseAdapter {

    private Context context;
    private List<OrderRecord.Good> goodList;
    private LayoutInflater inflater;
    private OnTextClickListener onTextClickListener;
    private boolean unEvaluate = true; //未评价
    private String status;

    public OrderSingleListAdapter(Context context, List<OrderRecord.Good> goodList, String status,
                                  OnTextClickListener onTextClickListener) {
        this.context = context;
        this.goodList = goodList;
        inflater = LayoutInflater.from(context);
        this.status = status;
        this.onTextClickListener = onTextClickListener;
    }

    @Override
    public int getCount() {
        return goodList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.lv_item_order_single, parent, false);
        }

        ImageView iv_pic = ViewHolder.get(convertView, R.id.iv_pic);
        TextView tv_name = ViewHolder.get(convertView, R.id.tv_name);
        TextView tv_price = ViewHolder.get(convertView, R.id.tv_price);
        TextView tv_evaluate = ViewHolder.get(convertView, R.id.tv_evaluate);

        OrderRecord.Good goods = goodList.get(position);
        String imgPath = Config.Api.img_url + goods.getGoods_pic();
        String name = goods.getName();
        String price = goods.getPrice_one();

        x.image().bind(iv_pic, imgPath);
        tv_name.setText(name);
        tv_price.setText(price);
        switch (status) {
            case "8":
            case "9":
            case "11":
            case "12":
            case "13":
                if (1 == goods.getGoods_evaluate()) {
                    tv_evaluate.setVisibility(View.VISIBLE);
                    tv_evaluate.setText("评价");
                } else {
                    tv_evaluate.setVisibility(View.VISIBLE);
                    tv_evaluate.setText("已评价");
                    unEvaluate = false;
                }
                break;
        }

        if (unEvaluate) {
            tv_evaluate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onTextClickListener.onEvaluate(position);
                }
            });
        }

        return convertView;
    }

    public interface OnTextClickListener {
        void onEvaluate(int position);
    }
}
